package com.zpark.controller;

import com.github.pagehelper.PageInfo;
import com.zpark.domain.User;
import com.zpark.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.sql.DataSource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
//@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("toupdate/{id}")
    public String toupdate(Model model, @PathVariable Long id){
        // 根据id查找到，我们要修改的那条用户记录，
        User user = userService.queryById(id);
        // 封装user
        model.addAttribute("user", user);
        // 并导航到update.html页面
        return "user/update";
    }
    // 添加一个修改用户的方法
    @GetMapping("update")
    public String update(User user, Model model){
        System.out.println("拿到前台传递过来的数据 user -> " + user);
        System.out.println("准备调用后台修改用户信息业务...");
        userService.update(user);
//        model.addAttribute("user", userService.queryById(user.getId()));
//        return "user/show";
        // 导航到查询模块
        return "redirect:/showAll";
    }

    @Autowired
    DataSource dataSource;
    @GetMapping("/show/{id}")
    public String show(Model model, @PathVariable Long id){
        User user = userService.queryById(id);
        System.out.println(dataSource);
        model.addAttribute("user", user);
        return "user/show";
    }

    // 导航到新添页面的方法
    @RequestMapping("toinsert")
    public String toInsert() {
        System.out.println("准备跳转到新用户添加页...");
        return "user/insert";
    }
    // 新添
    @RequestMapping("insert")
    public String insert(User user) {
        System.out.println("拿到前台传递过来的数据 user -> " + user);
        userService.saveUser(user);
        return "redirect:/show/" + user.getId();
    }

    // 查找所有
    @RequestMapping("showAll")
    public String showAll(Model model){
        System.out.println("从后台获取到全部数据，跳转到用户列表页...");
        List<User> userList = userService.queryAll();
        model.addAttribute("userList", userList);
        return "user/showAll";
    }

    // 根据id删除user
    @RequestMapping("delete/{id}")
    public String deleteById(@PathVariable Long id){
        System.out.println("准备执行删除业务，删除id为 " + id + "的用户");
        userService.deleteById(id);
        return "redirect:/showAll";
    }

    // 处理日期问题
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

    @Value("${page.size}")
    String pageSize;
    @Autowired
    com.zpark.serviceinterface.UserService userServiceImpl;

    @RequestMapping("queryByPage/{curPage}")
    public String queryByPage(@PathVariable("curPage") Integer curPage, Model model){
        if (curPage < 1)
            curPage = 1;
        PageInfo<User> pageInfo = userServiceImpl.queryByPage(curPage, Integer.valueOf(pageSize));
        model.addAttribute("pageInfo", pageInfo);
        return "user/showByPage";
    }

}
